package permissions

import (
	"context"
	"goAdminV1/api/permissions/v1"
	"goAdminV1/internal/model"
	"goAdminV1/utility"
)

func (c *ControllerV1) DepartmentList(ctx context.Context, req *v1.DepartmentListReq) (res *v1.DepartmentListRes, err error) {
	data, err := model.DepartmentsGetList(ctx, req)
	if err != nil {
		return nil, utility.Error(utility.CodeOperation, err)
	}

	// 如果 data.Data 是 nil，则设置为空数组
	if data.Data == nil {
		data.Data = []*v1.DepartmentListItem{}
	}

	res = &v1.DepartmentListRes{
		PaginatedResponse: *utility.SuccessWithPagination(
			data.Data,  // 实际数据
			req.Limit,  // 每页条数
			data.Total, // 总条数
			"部门列表获取成功", // 自定义成功消息
		),
	}
	return res, nil
}
